import request from '@/utils/request';

// 账号-分页查询 Api

export function reqGetList(data) {
    return request({
        url: '/drug/user/list',
        method: 'post',
        data
    })
}

// 查询可用角色列表
export function reqGetRoleList() {
    return request({
        url: '/drug/role/findRoleByUser',
        method: 'get'
    })
}

// 新增 || 修改
export function reqAddOrEdit(data) {
    if (data.id) {
        return request({
            url: '/drug/user/update',
            method: 'post',
            data
        })
    } else {
        return request({
            url: '/drug/user/save',
            method: 'post',
            data
        })
    }
}

// 删除账号信息
export function reqDelete(id) {
    return request({
        url: `/drug/user/delete/${id}`,
        method: 'delete'
    })
}

// 重置密码

export function reqResetPassword(id) {
    return request({
        url: `/drug/user/resetPassword/${id}`,
        method: 'put'
    })
}

//  文件下载接口
export function formidableUtil(res, fileName) {
    let blob = new Blob([res], {
        type: "application/vnd.ms-excel"
    })
    fileName = fileName || '下载.png';
    if ('download' in document.createElement('a')) {
        // 非IE下载
        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
    } else {
        // IE10+下载
        navigator.msSaveBlob(blob, fileName)
    }
}
// 下载人员管理数据

export function reqGetExport(data) {
    return request({
        url: '/drug/user/export',
        method: 'get',
        params: data,
        responseType: 'blob'
    }).then(res => {
        formidableUtil(res, '人员名单.xls')
    });
}